/**
 * @Title: SysResourceDaoJpa.java 
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">Psammead</a>
 * @date 2011-7-14 下午09:12:33 
 * @version 1.0
 * Copyright 2011 by Psammead
 */
package com.psammead.dms.system.sercurity.dao.jpa;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.psammead.dms.common.core.bo.Page;
import com.psammead.dms.common.core.dao.jpa.GenericDaoJpa;
import com.psammead.dms.system.sercurity.dao.SysResourceDao;
import com.psammead.dms.system.sercurity.entity.SysResource;

/**
 * 系统资源持久化实现类
 * @ClassName: SysResourceDaoJpa
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">psammead</a>
 * @version 1.0
 * @date 2011-7-14
 */
@Repository("sysResourceDao")
public class SysResourceDaoJpa extends GenericDaoJpa<SysResource, Long> implements SysResourceDao {

	public SysResourceDaoJpa(){
		super(SysResource.class);
	}

	/**
	 * 拼接查询条件
	 * @Title: sqlBuffer
	 * @Description: TODO
	 * @param paramMap
	 * @return StringBuffer
	 * @throws
	 */
	protected StringBuffer sqlMaker(Map<String, Object> paramMap){
		StringBuffer sb=new StringBuffer();
		if(!paramMap.keySet().isEmpty()){
			for(String key:paramMap.keySet()){
				if("name".equals(key)){
					sb.append(" and name like :name");
					paramMap.put(key, "%"+(String)paramMap.get(key)+"%");
				}
				if("type".equals(key)){
					sb.append(" and type = :type");
				}
				if("removed".equals(key)){
					sb.append(" and removed = :removed");
				}
			}
		}
		return sb;
	}
	
	/* (non-Javadoc)
	 * <p>Title: queryResultList</p>
	 * <p>Description: </p>
	 * @param jsql
	 * @param paramMap
	 * @param sortMap
	 * @return
	 * @see com.psammead.dms.common.core.dao.GenericDao
	 * 			#queryResultList(java.lang.String, java.util.Map, java.util.LinkedHashMap)
	 */
	@Override
	public List<SysResource> queryResultList(
			Map<String, Object> paramMap, LinkedHashMap<String, String> sortMap) {
		return this.queryResultList(new JsqlBuilder(){
			@Override
			public String buildJSql(Map<String, Object> paramMap) {
				StringBuffer sb= new StringBuffer();
				sb.append("select o from SysResource o where 1=1");
				sb.append(sqlMaker(paramMap));
				return sb.toString();
			}
		}, paramMap, sortMap);
	}

	/* (non-Javadoc)
	 * <p>Title: queryResultPage</p>
	 * <p>Description: </p>
	 * @param jSql
	 * @param countSql
	 * @param paramMap
	 * @param sortMap
	 * @param pageNo
	 * @param pageSize
	 * @return
	 * @see com.psammead.dms.common.core.dao.GenericDao
	 * 			#queryResultPage(java.lang.String, java.lang.String, java.util.Map, java.util.LinkedHashMap, int, int)
	 */
	@Override
	public Page<SysResource> queryResultPage(
			Map<String, Object> paramMap,
			LinkedHashMap<String, String> sortMap, int pageNo, int pageSize) {
		return this.queryResultPage(new SqlBuilder(){
			@Override
			public String buildJSql(Map<String, Object> paramMap) {
				StringBuffer sb= new StringBuffer();
				sb.append("select o from SysResource o where 1=1");
				sb.append(sqlMaker(paramMap));
				return sb.toString();
			}
			@Override
			public String buildCountSql(Map<String, Object> paramMap) {
				StringBuffer sb= new StringBuffer();
				sb.append("select count(o.id) from SysResource o where 1=1");
				sb.append(sqlMaker(paramMap));
				return sb.toString();
			}
		}, paramMap, sortMap, pageNo, pageSize);
	}

}
